package aggregate.aggregator;

import java.rmi.AccessException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

import aggregate.aggregator.remote.AggregatorRemoteInterface;

/**
 * @author andreivajnaii
 * Creates the Aggregator service and exports it
 */
public class AggregatorRemoteService {
	
	public static void main(String args[]) {
		try {
			Aggregator aggregator = startAggregator();		
			VerifyUpdatesThread thread = new VerifyUpdatesThread(aggregator);
			thread.start();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static Aggregator startAggregator()
			throws RemoteException, AccessException {
		Database db = new Database();
		Aggregator aggregator = new Aggregator(db);
		Remote aggregatorStub = (AggregatorRemoteInterface)
			UnicastRemoteObject.exportObject(aggregator, 0);
		Registry registry = LocateRegistry.createRegistry(5001);
		registry.rebind("Aggregator", aggregatorStub);
		System.out.println("Aggregator running");
		return aggregator;
	}

}
